//
//  HomeViewController.m
//  网易新闻
//
//  Created by mac on 16/7/19.
//  Copyright © 2016年 xbk. All rights reserved.
//

#import "HomeViewController.h"
#import "BKChannelModel.h"
#import "BKChannel.h"
#import "HomeCollectionViewCell.h"


@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
//频道
@property (weak, nonatomic) IBOutlet UIScrollView *channelScrollView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *collectionCollectionViewFlowLayout;
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;

//用于存放频道数据的数组.
@property (nonatomic,copy) NSArray *arrLabel;

@property (nonatomic,strong) NSMutableArray *labM;

@end

@implementation HomeViewController

- (NSMutableArray *)labM{

    if (_labM == nil) {
        
        _labM = [NSMutableArray array];
    }
    return _labM;
}
- (NSArray *)arrLabel{

    if (_arrLabel == nil) {
        _arrLabel = [NSMutableArray array];
    }
    return _arrLabel;
}

- (void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];
    //设置item的大小
    self.collectionCollectionViewFlowLayout.itemSize = self.myCollectionView.bounds.size;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   // NSLog(@"%@",[BKChannelModel channels]);
    
    // 提示 : 当UIScrollView和他的子类(UITableView,UICollectionView...),遇到导航控制器的时候,里面的类容会自动的向下偏移64个像素
    // PS : 控制器中也可以修改
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //创建频道标签
    [self creatChannelLabel];
    
}

- (void)creatChannelLabel{
    
    //1.获取标签模型内的数据
    self.arrLabel = [BKChannelModel channels];
    
    //2.遍历数组创建标签.
    
    CGFloat channlLabelW = 80;
    CGFloat channlLabelH = self.channelScrollView.bounds.size.height;
    
    for (int i=0; i<self.arrLabel.count; ++i) {
        
        BKChannel *channelLabel = [[BKChannel alloc]init];
        
        CGFloat channlLabelX = channlLabelW *i;
        
        channelLabel.frame = CGRectMake(channlLabelX, 0, channlLabelW, channlLabelH);
        
        //设置channelLabel的tag
        channelLabel.tag = i;
        
        //设置频道的滚动范围
        self.channelScrollView.contentSize = CGSizeMake(channlLabelW *self.arrLabel.count, 0);

        //3.给标签赋值
        BKChannelModel *model = self.arrLabel[i];
        channelLabel.text = model.tname;
        
        [self.labM addObject:channelLabel];
        
        [self.channelScrollView addSubview:channelLabel];
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
        
        [channelLabel addGestureRecognizer:tap];
        
        channelLabel.userInteractionEnabled = YES;
    }
    

}
#pragma mark - 点击频道的方法

- (void)tapClick:(UITapGestureRecognizer *)tap{

    BKChannel *lab = (BKChannel*)tap.view;
    
    //设置偏移量
    CGFloat labOffsetX = lab.center.x - self.view.bounds.size.width/2;
    
    //限制最大和最小偏移量
    CGFloat minOffsetX = 0;
    CGFloat maxOffsetX = self.channelScrollView.contentSize.width - self.view.bounds.size.width;
    
    if (labOffsetX < minOffsetX) {
        
        labOffsetX = 0;
    }else if(labOffsetX > maxOffsetX){
    
        labOffsetX = maxOffsetX;
    }
    
    //设置频道的滚动范围
    [self.channelScrollView setContentOffset:CGPointMake(labOffsetX, 0) animated:YES];
    
#pragma mark - 点击频道标签居中时,collectionview联动
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:lab.tag inSection:0];
    [self.myCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:0 animated:YES];
}

#pragma mark - collection代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.arrLabel.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0];
    
    BKChannelModel *model = self.arrLabel[indexPath.row];
    //NSLog(@"%@",model.tid);
    
    //NSLog(@"%ld",(long)indexPath.row);
    
    NSString *urlStr = [NSString stringWithFormat:@"article/list/%@/0-20.html",model.tid];
    
    //给HomeCollectionViewCell传值
    cell.urlStr = urlStr;
    
    return cell;
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    
    BKChannel *lab = self.labM[index];
    
    //lab滚动的偏移量
    CGFloat labOffSetX = lab.center.x - self.view.bounds.size.width/2;
    // 限制最大和最小的偏移量
    CGFloat minOffsetX = 0;
    CGFloat maxOffsetX = self.myCollectionView.contentSize.width - self.view.bounds.size.width;
    
    if (labOffSetX < minOffsetX) {
        labOffSetX = 0;
    }else if(labOffSetX > maxOffsetX)
    {
        labOffSetX = maxOffsetX;
    }
    
    [self.channelScrollView setContentOffset:CGPointMake(labOffSetX, 0) animated:YES];

}

@end
